package com.aaa.springboot_rbac.controller;

import com.aaa.springboot_rbac.entity.User;
import com.aaa.springboot_rbac.service.UserService;
import com.aaa.springboot_rbac.util.Page;
import com.aaa.springboot_rbac.util.Result;
import com.aaa.springboot_rbac.util.ResultUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @FileName: UserController
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/1 17:09
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     *  动态sql，分页查询
     * @param page
     * @return
     */
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<User> page) {
        return ResultUtil.success(userService.queryByPage(page));
    }

    /**
     * 根据id查询
     * @param userId
     * @return
     */
    @GetMapping("queryById")
    public Result queryById(Integer userId) {
        return ResultUtil.success(userService.queryById(userId));
    }

    /**
     * 添加
     * @param user
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody User user) {
        return ResultUtil.success(userService.insert(user));
    }

    /**
     * 根据id更新
     * @param user
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody User user) {
        return ResultUtil.success(userService.update(user));
    }

    /**
     * 根据id删除
     * @param userId
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer userId) {
        return ResultUtil.success(userService.deleteById(userId));
    }

    /**
     * 登录
     * @param userName
     * @param password
     * @return
     */
    @GetMapping("login")
    public Result login(String userName,String password) {
        return userService.checkUser(userName,password);
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("user");
        session.invalidate();
        return ResultUtil.success("退出成功");
    }
}
